﻿<?php

/*   Description  of this file
*    Author king-of-war
*    Last Modified 2009-4-8
*    This class designed  for the news operation
*/
class news {

	private $conn;
	private $error;
	private $url;

	function __construct($url) {

		/*
		 * load connection class
		 */
		include_once $url . 'includes/conn.php';
		$conn = new conn();
		$this->set_conn($conn);
		/*
		 * load error report class
		 */
		include_once $url . 'admin/class/error.class.php';
		$error = new Errmsg();
		$this->set_error($error);
		$this->set_url($url);

	}

	/*
	 * defin the function for news add
	 *
	 * $fun=1 means add
	 * $fun=2 means update
	 *
	 */

	function news_add($id, $title, $image, $comments, $author, $verify, $fun) {
		$conn = $this->get_conn();
		$error = 'null';
		if (!empty ($fun)) {

			if ($fun == '1') {
				/*
				 * add news sql
				 *
				 */
				$sql = sprintf("INSERT INTO news (id,title,images,comments,author,date,verify)VALUES(null,'%s','%s','%s','%s',NOW(),'%d')", $title, $image, $comments, $author, $verify);
				$error = 'add news error';
			}
			elseif ($fun == '2' && !empty ($id)) {
				/*
				* update news sql
				*/
				$sql = sprintf("update news set title='%s',images='%s',comments='%s',author='%s',verify='%d' where id='%s'", $title, $image, $comments, $author, $verify, $id);

				$error = 'update news error';

			}
			elseif ($fun == '3' && !empty ($id)) {

				/*
				 *   delete news sql
				 */

				$sql = sprintf("delete from news where id='%d'", $id);

				$error = 'delete news error';
			}

			$conn->set_sql($sql);

			$line = $conn->get_line();

			return $line;

		} else {

			return $line = 0;

			echo $error;
		}

	}
	/*
	 * define news_get function
	 *
	 * $id is the id of the data which you need
	 *
	 * if $id is array , the function will get result as array
	 *
	 * $fun means admin   select with all or limit
	 *
	 */

	function news_get($id, $fun) {

		$conn = $this->get_conn();
		$url = $this->get_url();
		include_once $url . 'includes/entity/news.entity.php';
		$info = '';
		if (!$fun) {
			$info = ' where verify =1 ';
		}

		if (is_array($id)) {
			/*
			 * return the resluts as array
			 * $a means start   $b means counts
			 */
			$a = $id[0];
			$b = $id[1];
			$sql = sprintf("select * from news " . $info . "limit %d,%d ", $a, $b);

		} else {
			/*
			 *  reslut is only one
			 */
			$sql = sprintf("select * from news where id='%d' " . $info, $id);

		}

		$conn->set_sql($sql);

		$line = $conn->get_line();

		if ($line) {
			$result = $conn->get_result();
			for ($i = 0; $row = $result->fetch_assoc(); $i++) {
				$news[$i] = new news_entity();
				$id = $row['id'];
				$title = $row['title'];
				$image = $row['images'];
				$comments = $row['comments'];
				$author = $row['author'];
				$date = $row['date'];
				$verify = $row['verify'];
				$news[$i]->set_id($id);
				$news[$i]->set_title($title);
				$news[$i]->set_images($image);
				$news[$i]->set_comments($comments);
				$news[$i]->set_author($author);
				$news[$i]->set_date($date);
				$news[$i]->set_verify($verify);
			}

		} else {
			echo '暂无新闻';
			$news = null;
		}
		return $news;
	}

	/*
	 *   defin news delete function
	 *
	 *   delete news with id or ids
	 */

	function news_del($ids) {

		if (isset ($ids)) {

			if (is_array($ids)) {
				$max = count($ids);
				for ($i = 0; $i<$max; $i++) {
					$conn = new conn();

					$sql = sprintf("delete from news where id=%d ; ",$ids[$i]);

				    $conn->set_sql($sql);

			        $line1 = $conn->get_line();

				    $line+=$line1;
				}

			} else {
				$conn = new conn();
				$sql = sprintf("delete from news where id=%d", $ids);

			$conn->set_sql($sql);

			$line = $conn->get_line();
			}


		} else {
			echo 'at least choose one for process please';
			$line = 0;

		}
		return $line;
	}

	/*
	 * functions for use above
	 */
	function set_conn($conn) {
		$this->conn = $conn;
	}
	function get_conn() {
		return $this->conn;
	}
	function set_error($error) {
		$this->error = $error;
	}
	function get_error() {
		return $this->error;
	}

	function set_url($url) {
		$this->url = $url;
	}
	function get_url() {
		return $this->url;
	}
}
?>